package com.tanhua.sso.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.tanhua.common.config.AliyunOSSConfig;
import com.tanhua.common.service.PicUploadService;
import com.tanhua.common.utils.JwtUtils;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.common.vo.PicUploadResult;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.enums.SexEnum;
import com.tanhua.dubbo.pojo.UserInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Service
public class UserInfoService {
    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;
    @Autowired
    private RSAService rsaService;
    @Autowired
    private PicUploadService uploadService;
    @Autowired
    private FaceImageService faceImageService;
    @Autowired
    private OSS oss;
    @Autowired
    private AliyunOSSConfig aliyunOSSConfig;
    //填写资料
    public Object loginRegInfo(Map<String, String> param,String token) {
        //先判断token是否合法
        Map<String, Object> map = JwtUtils.checkToken(token, this.rsaService.getPublicKeyStr());
        if (MapUtil.isEmpty(map)){
            return ErrorResult.builder().errCode("401").errMessage("抱歉登陆已过期,请重新登录");
        }

        if (ObjectUtil.isEmpty(map.get("userId"))){
            return ErrorResult.builder().errCode("401").errMessage("抱歉您的信息有误,请重新登录");
        }
        //保存用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setNickName(param.get("nickname"));
        userInfo.setSex(StrUtil.equals(param.get("gender"),"man")?SexEnum.MAN:SexEnum.WOMAN);
        userInfo.setBirthday(param.get("birthday"));
        userInfo.setCity(param.get("city"));
        userInfo.setUserId(Convert.toLong(map.get("userId")));
        Boolean result = this.userInfoApi.save(userInfo);
        if (result){
            return null;
        }
        return ErrorResult.builder().errCode("500").errMessage("抱歉用户信息更新失败,请检查数据!");
    }
    //用户上传头像
    public Object loginRegInfoHead(String token, MultipartFile multipartFile) {
        //先判断token是否合法
        Map<String, Object> map = JwtUtils.checkToken(token, this.rsaService.getPublicKeyStr());
        if (MapUtil.isEmpty(map)){
            return ErrorResult.builder().errCode("500").errMessage("抱歉登陆已过期,请重新登录");
        }

        if (ObjectUtil.isEmpty(map.get("userId"))){
            return ErrorResult.builder().errCode("500").errMessage("抱歉您的信息有误,请重新登录");
        }

        //先上传头像再进行校验是不是人像
        PicUploadResult uploadResult = this.uploadService.upload(multipartFile);
        if (StrUtil.isEmpty(uploadResult.getName())){
            return ErrorResult.builder().errCode("500").errMessage("抱歉您上传的头像不符合规范,请重新上传").build();
        }
        //校验上传的头像是否为人像
        boolean result = this.faceImageService.checkIsPortrait(uploadResult.getName());
        if (result){
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId(Convert.toLong(map.get("userId")));
            userInfo.setLogo(uploadResult.getName());
            userInfo.setCoverPic(uploadResult.getName());
            Boolean flag = this.userInfoApi.update(userInfo);
            if (flag){
                return null;
            }
            return ErrorResult.builder().errCode("500").errMessage("抱歉头像上传失败,请重新上传").build();
        }
        //TODO
        oss.deleteObject(aliyunOSSConfig.getBucketName(),StrUtil.removePrefix(uploadResult.getName(),aliyunOSSConfig.getUrlPrefix()));
        return ErrorResult.builder().errCode("500").errMessage("抱歉您上传的头像不是人像,请重新上传").build();
    }
}
